﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VirtualPedal;

namespace VPApp
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		AudioDevicePanel m_InputPanel;
		AudioDevicePanel m_OutputPanel;

		double m_CanvasAvailablePos;

		public MainWindow()
		{
			InitializeComponent();
			CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
			m_CanvasAvailablePos = 0;
		}

		void CompositionTarget_Rendering(object sender, EventArgs e)
		{
			waveView1.Render();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			m_InputPanel = new AudioDevicePanel(AudioDevicePanel.EDeviceType.Capture);
			panelBk.Children.Add(m_InputPanel);
			m_OutputPanel = new AudioDevicePanel(AudioDevicePanel.EDeviceType.Render);
			panelBk.Children.Add(m_OutputPanel);
			UpdateLayout();
			Canvas.SetLeft(m_InputPanel, 0);
			Canvas.SetRight(m_OutputPanel, 0);//m_InputPanel.ActualWidth);
			
			m_CanvasAvailablePos = m_InputPanel.ActualHeight + 5;

			m_InputPanel.DeviceStateChanged += new EventHandler<EventArgs>((s1, e2) =>
			{
				if (m_InputPanel.Device.IsOpened)
				{
					(m_OutputPanel.Device as AudioRenderer).SetDataPort(
						(m_InputPanel.Device as AudioSource).GetDataPort());
					waveView1.BindPort((m_InputPanel.Device as AudioSource).GetDataPort());
				}
				m_OutputPanel.IsEnabled = m_InputPanel.Device.IsOpened;
			});
			m_OutputPanel.IsEnabled = false;
			
			/*
			var overdrive = new OverdrivePedal();
			(m_InputPanel.Device as AudioSource).AddPedal(overdrive);
			var distortion = new DistortionPedal();
			(m_InputPanel.Device as AudioSource).AddPedal(distortion);
			var shelving = new ShelvingPedal();
			(m_InputPanel.Device as AudioSource).AddPedal(shelving);
			*/

			AddPedal<OverdrivePedal>();
			AddPedal<OverdrivePedal>();
			AddPedal<DistortionPedal>();
			
			
			//AddPedal<ShelvingPedal>();
		}

		void AddPedal<T>() where T : PedalBase, new()
		{
			var pedalDevice = new T();
			(m_InputPanel.Device as AudioSource).AddPedal(pedalDevice);
			PedalController controller = new PedalController(pedalDevice);
			panelPd.Children.Add(controller);
			//Canvas.SetTop(controller, m_CanvasAvailablePos);
			UpdateLayout();
			//m_CanvasAvailablePos += controller.ActualHeight + 5;
		}

		private void Window_Closed(object sender, EventArgs e)
		{
			if (m_InputPanel.Device.IsOpened)
				m_InputPanel.Device.Close();
			if (m_OutputPanel.Device.IsOpened)
				m_OutputPanel.Device.Close();
		}

		private void menuDistortion_Click(object sender, RoutedEventArgs e)
		{
			
		}

		private void menuOverdrive_Click(object sender, RoutedEventArgs e)
		{

		}
	}
}